<template xmlns="http://www.w3.org/1999/html">
  <sea-data-table ref="tableObject" url="/schedule/broad/page">
    <!--表头-->
    <sea-table-header slot="header">
      <!--标题栏-->
      <sea-table-title slot="title" title="定时广播" subtitle="与quartz完全解耦的用法，代码更加简洁清晰。"></sea-table-title>

      <!--按钮栏-->
      <template slot="buttons">
        <el-button plain size="small" title="新增" icon="fa fa-plus" @click="onAction('new')"></el-button>
      </template>
    </sea-table-header>

    <!-- 弹窗、抽屉 -->
    <template slot="drawer">
    </template>

    <!--表头-->
    <template slot="columns">
      <el-table-column property="groupName" label="事件分组" width="180"></el-table-column>
      <el-table-column property="eventName" label="事件名称" width="180"></el-table-column>
      <sea-col-input property="cron" label="表达式" width="180"></sea-col-input>
      <sea-col-switch property="disabled" label="是否禁用" width="180" :change="onAction" reverse></sea-col-switch>
      <el-table-column property="description" label="功能描述"></el-table-column>

      <!--操作栏-->
      <sea-table-actionbar width="220" :on-action="onAction" :buttons="buttons"></sea-table-actionbar>
    </template>

    <!-- 弹窗、抽屉 -->
    <template slot="drawer">
      <sea-dialog ref="history_dialog">
        <history></history>
      </sea-dialog>
    </template>
  </sea-data-table>
</template>

<script>
import history from "./history";

export default {
    components: {history},
    data() {
        return {
            // 操作栏
            buttons: [
                {type: 'btn', evt: 'next_fire', label: 'Next Fire', title: '下一次触发时间'},
                {type: 'divider'},
                {type: 'btn', evt: 'fire', label: 'Fire', title: '立即触发'}
            ]
            // end--
        }
    },
    methods: {
        /**
         * 操作成功回调
         */
        onSucceed: function () {
            this.$refs.tableObject.reload();
        },
        /**
         * 操作栏事件分发
         * @param evt 事件
         * @param row 行级数据
         * @param old 可供回滚的数据
         */
        onAction: function (evt, row, old) {
            if (evt === '$edit') {
                let url = row.disabled ? '/schedule/broad/pause' : '/schedule/broad/resume';
                this.$layers.ajax({
                    method: "PUT",
                    url: url,
                    params: {id: row.id}
                }).then(result => {
                    if (result.code === 0) {
                        this.$layers.succeed();
                    }
                });
            } else if ('next_fire' === evt) {
                this.$layers.ajax({
                    method: "GET",
                    url: "/schedule/job/nextFireTime",
                    params: {cron: row.cron}
                }).then(result => {
                    if (result.code === 0) {
                        let data = result.data;
                        if (data.length === 0) {
                            this.$layers.warn('配置正确，但是当前时间大于最后一次触发时间!');
                        } else {
                            for (let i = 0; i < data.length; i++) {
                                data[i] = '<p>' + data[i] + '</p>';
                            }
                            this.$layers.alert('未来5次的触发时间', data.join(''));
                        }
                    }
                });
            } else if ('fire' === evt) {
                let row_data = row;
                if (row_data.disabled === true) {
                    this.$layers.failed('该计划任务已禁用，无法触发！');
                    return;
                }
                this.$layers.ajax({
                    method: "GET",
                    url: "/schedule/broad/trigger",
                    params: row_data
                }).then(result => {
                    if (result.code === 0) {
                        this.$layers.succeed();
                    }
                });
            }
            // end
        }
    }
}
</script>
